package com.wwc.sc.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wwc.sc.entities.Payment;
import com.wwc.sc.entities.result.CommonResult;
import com.wwc.sc.service.OrderHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author WWC
 * 服务降级全局处理 推荐使用：OrderHystrixFallBackServiceImpl 实现类全局处理
 *
 * 服务降级全局处理：
 * 1，注解，@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
 * 2，对应的兜底方法 payment_Global_FallbackMethod
 * 3，该方法上加上 @HystrixCommand 注解
 *
 * 服务降级单一处理：
 * 1，在方法上加上
 * @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
 *    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
 * })
 * 2，对应的兜底方法 paymentTimeOutFallbackMethod
 *
 */
@RestController
@Slf4j
@RequestMapping(value = "/consumer/payment/hystrix")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private OrderHystrixService orderHystrixService;

    @GetMapping("/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = orderHystrixService.paymentInfo_OK(id);
        log.info("****result: "+result);
        return result;
    }

    @GetMapping("/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        int age = 10/0;
        String result = orderHystrixService.paymentInfo_TimeOut(id);
        log.info("****result: "+result);
        return result;
    }

    public String paymentTimeOutFallbackMethod() {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试，/(ㄒoㄒ)/~~";
    }


}
